package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.pojo.entity.BaseCheckStockEntity;
import com.kaili.basic.pojo.entity.BaseStoreStockEntity;
import com.kaili.basic.pojo.vo.*;
import com.kaili.basic.service.BaseCheckStockService;
import com.kaili.basic.service.BaseStoreStockService;
import com.kaili.common.response.RestStatus;
import com.kaili.basic.mapper.BaseCheckStoreMapper;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.BaseCheckStoreDetailEntity;
import com.kaili.basic.pojo.entity.BaseCheckStoreEntity;
import com.kaili.basic.service.BaseCheckStoreDetailService;
import com.kaili.basic.service.BaseCheckStoreService;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@Service
@Transactional
public class BaseCheckStoreServiceImpl extends ServiceImpl<BaseCheckStoreMapper, BaseCheckStoreEntity> implements BaseCheckStoreService {

    @Autowired
    private BaseCheckStoreMapper baseCheckStoreMapper;

    @Autowired
    private BaseCheckStoreDetailService baseCheckStoreDetailService;

    @Autowired
    private BaseCheckStockService baseCheckStockService;

    @Autowired
    private BaseStoreStockService baseStoreStockService;

    @Override
    public Long queryStoreOrderNo() {
        return baseCheckStoreMapper.queryStoreOrderNo();
    }

    @Override
    public RestStatus addCheckStock(BaseCheckStoreAddDTO addDto) {
        //新增仓库盘点单
        BaseCheckStoreEntity entity=new BaseCheckStoreEntity();
        BeanUtils.copyProperties(addDto,entity);
        entity.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        entity.setCreateTime(new Date());
        entity.setCheckStoreType(261L);
        this.insert(entity);

        //仓库盘点明细
        List<BaseCheckStoreDetailAddDTO> list = addDto.getList();
        for (BaseCheckStoreDetailAddDTO detail:list) {
            List<BaseStoreStockEntity> stockList= baseStoreStockService.selectByMap(new HashMap<String, Object>() {{
                put("store_id", detail.getStoreId());
                put("product_id", detail.getProductId());
            }});
            baseCheckStoreDetailService.insertBath(stockList,detail);
        }

        return CommonStatus.ADD_OK;
    }


    @Override
    public RestStatus updateCheckStock(BaseCheckStoreEditDTO editDTO) {
        //新增仓库盘点单
        BaseCheckStoreEntity entity=new BaseCheckStoreEntity();
        BeanUtils.copyProperties(editDTO,entity);
        entity.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        entity.setModifyTime(new Date());
        this.updateById(entity);
       //清除原有仓库盘点明细
        baseCheckStoreDetailService.deleteByMap(new HashMap<String,Object>(){{put("order_no",editDTO.getOrderNo());}});
        //新增仓库盘点明细
        List<BaseCheckStoreDetailAddDTO> list = editDTO.getList();
        for (BaseCheckStoreDetailAddDTO detail:list) {
            List<BaseStoreStockEntity> stockList= baseStoreStockService.selectByMap(new HashMap<String, Object>() {{
                put("store_id", detail.getStoreId());
                put("product_id", detail.getProductId());
            }});
            baseCheckStoreDetailService.insertBath(stockList,detail);
        }
        return CommonStatus.UPDATE_OK;
    }

    @Override
    public RestStatus auditCheckStock(BaseCheckStoreAuditDTO auditDTO) {
        //已经做过审核不做处理
        if(!("0".equals(this.selectById(auditDTO.getId()).getIsAuduit()))){
            return CommonStatus.UPDATE_OK;
        }
        BaseCheckStoreEntity entity=new BaseCheckStoreEntity();
        BeanUtils.copyProperties(auditDTO,entity);
        this.updateById(entity);
        return CommonStatus.UPDATE_OK;
    }

    @Override
    public RestStatus uploadCheckStock(List<BaseCheckStockAddDTO> addList) {
        //更新盘点单为已完成状态
        baseCheckStoreMapper.updateIsFinish(addList.get(0).getOrderNo());

        for (BaseCheckStockAddDTO addDTO:addList) {
            //新增盘点上传产品
            BaseCheckStockEntity entity = new BaseCheckStockEntity();
            BeanUtils.copyProperties(addDTO, entity);
            entity.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
            entity.setCreateTime(new Date());
            baseCheckStockService.insert(entity);
            //如果库存中存在的盘点信息进行库存差异更新
            if(addDTO.getCheckStockDetailId()!=null){
                BaseCheckStoreDetailEntity detail = baseCheckStoreDetailService.selectById(addDTO.getCheckStockDetailId());
                //设置设置实际盘点数据
                detail.setGrossAmount(entity.getAmount());
                detail.setGrossWeight(entity.getWeight());
                detail.setGrossVolume(entity.getVolume());
                detail.setGrossTrays(entity.getTrays());
                //设置差异库存数据
                detail.setDifferAmount(detail.getPaperAmount() - entity.getAmount());
                detail.setDifferWeight(detail.getPaperWeight() - entity.getWeight());
                detail.setDifferVolume(detail.getPaperVolume() - entity.getVolume());
                detail.setDifferTrays(detail.getPaperTrays() - entity.getTrays());
                //更新上传后信息
                baseCheckStoreDetailService.updateById(detail);
            }
            //如果库存中不存在的则直接新增
            else{
                BaseCheckStoreDetailEntity detail=new BaseCheckStoreDetailEntity();
                //设置设置实际盘点数据
                detail.setGrossAmount(entity.getAmount());
                detail.setGrossWeight(entity.getWeight());
                detail.setGrossVolume(entity.getVolume());
                detail.setGrossTrays(entity.getTrays());
                //设置差异库存数据
                detail.setDifferAmount(-entity.getAmount());
                detail.setDifferWeight(-entity.getWeight());
                detail.setDifferVolume(-entity.getVolume());
                detail.setDifferTrays(-entity.getTrays());
                //设置上传信息
                detail.setOrderNo(entity.getOrderNo());
                detail.setBatch(entity.getBatch());
                detail.setDeadTime(entity.getDeadTime());
                detail.setLocationId(entity.getLocationId());
                detail.setStoreId(entity.getStoreId());
                detail.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
                detail.setCreateTime(new Date());
                baseCheckStoreDetailService.insert(detail);
            }

        }
        return CommonStatus.ADD_OK;
    }

    @Override
    public Page<BaseCheckStoreVO> queryPageList(Page<BaseCheckStoreVO> page, BaseCheckStoreQueryDTO queryDto) {
        List<BaseCheckStoreVO> list=baseCheckStoreMapper.queryPageList(page,queryDto);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<BaseCheckStoreExportVO> checkStoreExport(BaseCheckStoreQueryDTO queryDto) {
        return baseCheckStoreMapper.checkStoreExport(queryDto);
    }

    @Override
    public Page<BaseCanCheckStockVO> queryCanCheckStock(Page<BaseCanCheckStockVO> page,BaseCanCheckStockDTO queryDto) {
        page.setRecords(baseCheckStoreMapper.queryCanCheckStock(page,queryDto));
        return page;
    }

    @Override
    public BaseCheckStoreVO queryCheckStockById(Long id) {
        BaseCheckStoreVO storeVO = baseCheckStoreMapper.queryPageList(new HashMap<String, Object>() {{
            put("id",id);
        }});
        List<BaseCanCheckStoreProductVO> list=baseCheckStoreMapper.queryCanCheckStoreProduct(storeVO.getOrderNo());
        storeVO.setList(list);
        return storeVO;
    }

    @Override
    public BaseCheckStoreVO queryCheckStockAndDetail(Long id) {
        BaseCheckStoreVO storeVO = baseCheckStoreMapper.queryPageList(new HashMap<String, Object>() {{
            put("id",id);
        }});
        List<BaseCheckStoreDetailVO> list=baseCheckStoreMapper.queryCheckStockAndDetail(storeVO.getOrderNo());
        storeVO.setDetailList(list);
        return storeVO;
    }

    @Override
    public BaseCheckStoreVO queryCheckProductDetail(Long id) {
        BaseCheckStoreVO storeVO = baseCheckStoreMapper.queryPageList(new HashMap<String, Object>() {{
            put("id",id);
        }});
        List<BaseCanCheckStoreProductVO> list=baseCheckStoreMapper.queryCheckProductDetail(storeVO.getOrderNo());
        storeVO.setList(list);
        return storeVO;
    }
}
